package cn.bdqn.controller.questionBank;

import cn.bdqn.dto.LoginUser;
import cn.bdqn.dto.questionBank.subject.SubjectAccountTypeDTO;
import cn.bdqn.service.questionBank.IndustryService;
import cn.bdqn.utils.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/industry")
public class IndustryController {

    @Autowired
    private IndustryService industryService;

    /**
     * 更新行业信息
     * @param subjectAccountTypeDTO
     * @param servletRequest
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @PutMapping("/updateIndustryById")
    public Json updateIndustryById(@RequestBody SubjectAccountTypeDTO subjectAccountTypeDTO, HttpServletRequest servletRequest){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        try {
            return industryService.updateIndustryById(subjectAccountTypeDTO,userid);
        } catch (Exception e) {

            return Json.fall("添加失败");
        }
    }

    /**
     * 删除行业信息
     * @param industryId
     * @param httpServletRequest
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @DeleteMapping("/deleteIndustryById")
    public Json deleteIndustryById(Integer industryId,HttpServletRequest httpServletRequest){
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        try {
            //String idString = JwtUtil.parseJWT(httpServletRequest.getHeader("token")).getSubject();
            return industryService.deleteIndustryById(industryId,userid);
        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }

    }

    /**
     * 添加一条行业信息
     * @param subjectAccountTypeDTO
     * @param servletRequest
     * @return
     */
    @PreAuthorize("hasAuthority('question:management')")
    @PostMapping("/insertIndustry")
    public Json insertIndustry(@RequestBody SubjectAccountTypeDTO subjectAccountTypeDTO,HttpServletRequest servletRequest){
        //System.out.println(subjectAccountTypeDTO == null);
        LoginUser principal = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Integer userid = principal.getUser().getUserId();
        try {
            //String idString = JwtUtil.parseJWT(httpServletRequest.getHeader("token")).getSubject();
            return industryService.insertIndustry(subjectAccountTypeDTO,userid);
        } catch (Exception e) {
            return Json.fall(e.getMessage());
        }
    }
}
